const Type = require('./mainlib/type');
const core = require('./mainlib/core');
const {remote} = require('electron');

//默认文件类型图标
let defaultExtnameIcons = {
    '': {type:'svg', src:'#icon-file'},
    'dir':  {type:'svg', src:'#icon-folder'},
    '.exe':  {type:'svg', src:'#icon-exe'},
    '.bat' :  {type:'svg', src:'#icon-jiaoben1'},
    '.cmd' :  {type:'svg', src:'#icon-jiaoben1'},
    '.vbs' :  {type:'svg', src:'#icon-jiaoben1'},
    '.xls' :  {type:'svg', src:'#icon-xls'},
    '.xlsx' :  {type:'svg', src:'#icon-xls'},
    '.et' :  {type:'svg', src:'#icon-xls'},
    '.doc' :  {type:'svg', src:'#icon-doc'},
    '.docx' :  {type:'svg', src:'#icon-doc'},
    '.ppt' :  {type:'svg', src:'#icon-PPT'},
    '.pptx' :  {type:'svg', src:'#icon-PPT'},
    'img':  {type:'svg', src:'#icon-tupian'},
    'video':  {type:'svg', src:'#icon-shipin'},
    '.psd':  {type:'svg', src:'#icon-Ps'},
};

//可执行文件的后缀
let executeableExtnames = ['.exe', '.com', '.cmd', '.bat', '.vbs', '.lnk'];

//文件后缀对应类型
let extnameTypes = {
    '.png': 'img',
    '.bmp': 'img',
    '.ico': 'img',
    '.jpg': 'img',
    '.jpeg': 'img',
    '.gif': 'img',
    '.mp4': 'video',
    '.rm': 'video',
    '.rmvb': 'video',
    '.avi': 'video',
    '.wmv': 'video',
    '.mv4': 'video',
    '.mov': 'video',
    '.flv': 'video',
    '.asf': 'video',
    '.mpeg': 'video',
    '.mpg': 'video',
    '.vob': 'video',
    '.mkv': 'video',
};

let buildInFiles = {};
let iconCache = {};
let extnameIcons = {
    '.cpl': {type:'svg', src:'#icon-xitongshezhi'}
};

let FileType = {
    getExtnameDefaultIcon(extname) {
        let key = extnameTypes[extname] ? extnameTypes[extname] : extname;
        let icon = defaultExtnameIcons[key] ? defaultExtnameIcons[key] : defaultExtnameIcons[extname];
        
        if(!icon) {
            icon = defaultExtnameIcons[''];
        }
        
        return Type.clone(icon);
    },
    
    getDirDefaultIcon() {
        return this.getExtnameDefaultIcon('dir');
    },
    
    getExecuteableExtnames() {
        return Type.clone(executeableExtnames);
    },
    
    /**
     * 注册内置文件
     * @param {*} files 
     * @param {*} options 
     */
    registerBuildInFiles(files, options) {
        let _add = (file, options) => {
            let replace = options.replace;
            
            file = core.correctPath(file);
            options.path = file;
            
            if(options.link) {
                options.link = core.correctPath(options.link);
            }
            
            if(options.link == file) {
                delete options.link;
            }
            
            if(replace) {
                buildInFiles[file] = options;
            } else {
                buildInFiles[file] = Type.merge(buildInFiles[file]?buildInFiles[file]:{}, options);
            }
        };
        
        if(Type.isArray(files)) {
            for(let i in files) {
                _add(files[i].path, files[i]);
            }
        } else {
            _add(files, options);
        }
    },
    
    getBuildInFileInfo(file) {
        file = core.correctPath(file);
        
        if(!file) {
            return null;
        }
        
        let result = buildInFiles[file];
        
        if(result && result.link) {
            let linkResult = result;
            let cicle = {};
            cicle[linkResult.link] = true;
            result = null;
            
            while (true) {
                linkResult = buildInFiles[linkResult.link];
                
                if(!linkResult) {
                    return result;
                }
                
                if(cicle[linkResult.link]) {
                    return result;
                }
                
                result = linkResult;
            }
        }
        
        return result;
    },
    
    updateIcon(item) {
        if(item.icon && !item.icon.isdefault) {
            return item.icon;
        }
        
        let target = item.target;
        let targetExtname = item.targetExtname;
        let extkey = targetExtname ? `:${targetExtname}` : null;
        
        if(item.realIsDirectory) {
            return item.icon = this.getDirDefaultIcon();
        }
    
        let icon = this.getExtnameDefaultIcon(targetExtname);
    
        let buildInFileInfo = this.getBuildInFileInfo(item.path);
        buildInFileInfo = buildInFileInfo ? buildInFileInfo : this.getBuildInFileInfo(target);
    
        if(!buildInFileInfo && !item.extname && !item.targetExtname) {
            return item.icon = icon;
        }
    
        if(buildInFileInfo && buildInFileInfo.icon) {
            return item.icon = Type.clone(buildInFileInfo.icon);
        }
    
        let iconTarget = target;
    
        if(item.iconFetchFile && path.extname(item.iconFetchFile).toLowerCase() != '.dll') {
            iconTarget = core.correctPath(item.iconFetchFile);
        }
        
        let iconTargetExtname = targetExtname;
        let iconTargetExtkey = extkey;
    
        if(buildInFileInfo) {
            iconTarget = buildInFileInfo.path;
            iconTargetExtname = path.extname(iconTarget);
            iconTargetExtkey = iconTargetExtname ? `:${iconTargetExtname}` : null;
        }
        
        //读取自定义后缀配置图标
        if(extnameIcons[iconTargetExtname]) {
            return item.icon = Type.clone(extnameIcons[iconTargetExtname]);
        }
    
        //读取系统文件图标
        if(iconCache[iconTarget]) {
            return item.icon = Type.clone(iconCache[iconTarget]);
        }  else if(iconTargetExtkey && iconCache[iconTargetExtkey]) {
            return item.icon = Type.clone(iconCache[iconTargetExtkey]);
        } else {
            if(iconTargetExtname == '.exe' || !iconTargetExtkey) {
                iconTargetExtkey = iconTarget;
            }
        
            remote.app.getFileIcon(iconTarget, {size:'large'}).then(img => {
                if(!img.isEmpty()) {
                    iconCache[iconTargetExtkey] = {
                        type: 'img',
                        src: img.toDataURL()
                    };
    
                    return item.icon = Type.clone(iconCache[iconTargetExtkey]);
                } else {
                    return item.icon = icon;
                }
            });
        }
    },
    
    getExtnameType(ext) {
        return extnameTypes[ext];
    },
    
    getAllBuildInFiles() {
        return Type.clone(buildInFiles);
    },
};

module.exports = FileType;
